<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=request.getContextPath()%>/">
<meta charset="utf-8">
<title>我的订单</title>

<link rel="stylesheet" href="../layui/css/layui.css" media="all">
<script type="text/javascript" src="../layui/layui.js"></script>
<!-- 网页字体库 -->
<link href="http://fonts.googleapis.com/css?family=Poppins:100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i&amp;subset=devanagari,latin-ext"rel="stylesheet">
<link href="http://fonts.googleapis.com/css?family=Tangerine:400,700" rel="stylesheet">
<link href="http://fonts.googleapis.com/css?family=Roboto+Condensed:300,300i,400,400i,700,700i&amp;subset=cyrillic,cyrillic-ext,greek,greek-ext,latin-ext,vietnamese" rel="stylesheet">

<link rel="stylesheet" type="text/css" href="css/student/bootstrap.min.css">
<script type="text/javascript" src="js/student/jquery.min.js"></script>
<script type="text/javascript" src="js/student/popper.min.js"></script>
<script type="text/javascript" src="js/student/bootstrap.min.js"></script>
<style>
#order {
	font-family: '黑体';
	font-size: 18px;
	height: 40px;
	line-height: 40px;
}

#table {
	font-size: 16px;
	text-align: center;
}

#table td {
	padding: 3px;
	line-height: 51px;
}

#status {
	padding: 0;
	margin: 0;
	font-weight: 600;
}

td button {
	/* padding: 10px 15px; */
	margin-left: 6px;
}
</style>
</head>
<body>
	<div class="container">
	
		<!-- 导航栏 -->
		<ul class="layui-nav">
		  <li class="layui-nav-item">
		   <!-- 	<a href="../home.jsp" id="totle">Yoga</a> -->
		   	<a href="../home.jsp" style="font-family: 'Tangerine', cursive;font-size: 100%;color:white;">Yoga</a>
		  </li>
		</ul>
		
		<p class="bg-light" id="order">我的订单</p>
		<table class="table table-bordered table-hover" id="table">
			<thead>
				<tr class="table-light">
					<th>下 单 时 间</th>
					<th>订 单 号</th>
					<th>课 程 内 容</th>
					<th>学 员 姓 名</th>
					<th>订 单 金 额</th>
					<th>
						<div class="btn-group">
							<label class="btn dropdown-toggle" data-toggle="dropdown" id="status">全 部 状 态</label>
							<!-- 下拉菜单的内容 -->
							<div class="dropdown-menu">
								<a href="javascript:void(0)" class="dropdown-item" id="status_10">&nbsp;&nbsp;全 部 状 态</a>
								<a href="javascript:void(0)" class="dropdown-item" id="status_0">&nbsp;&nbsp;授 课 中</a> 
								<a href="javascript:void(0)" class="dropdown-item" id="status_1">&nbsp;&nbsp;授 课 完 毕</a> 
								<a href="javascript:void(0)" class="dropdown-item" id="status_2">&nbsp;&nbsp;交 易 完 成</a> 
								<a href="javascript:void(0)" class="dropdown-item" id="status_3">&nbsp;&nbsp;退款申请</a> 
								<a href="javascript:void(0)" class="dropdown-item" id="status_4">&nbsp;&nbsp;已 退 款</a>
								<a href="javascript:void(0)" class="dropdown-item" id="status_5">&nbsp;&nbsp;取消申请</a>
								<a href="javascript:void(0)" class="dropdown-item" id="status_6">&nbsp;&nbsp;已 取 消</a>
								<a href="javascript:void(0)" class="dropdown-item" id="status_7">&nbsp;&nbsp;已 拒 绝</a>
							</div>
						</div>
					</th>
					<th style="width:30%">操 作</th>
				</tr>
			</thead>
			<tbody id="tbody">
				<!-- 查询到的数据 -->
			</tbody>
		</table>
		<hr>
		<div id="page">
			&nbsp;共有<span id="total">0</span>条记录, 当前第<span id=currentPage>0</span>/
			<span id=totalPage>0</span>页 <span style="float: right; margin-right: 10px;">
				<button id="firstPage">首页</button>
				<button id="upPage">上一页</button>
				<button id="downPage">下一页</button>
				<button id="lastPage">末页</button> 
				跳转第<input id=jumpPage type="text" style="width:30px;"/>页
				<button id="jump">跳转</button>
			</span>
		</div>

	</div>
	<%@ include file="pay.jsp" %>
	<%@ include file="showComment.jsp" %> 
	<%-- <%@ include file="showComment.jsp" %> --%> 

	<script type="text/javascript">
			
			/* ------入口函数------- */
			$(function(){
				coachOrder(1,10);
			});
			
			
			/* ------页面按钮控制开始------ */
			//首页按钮
			$("#firstPage").click(function(){
				//获取当前页
				var currentPage = parseInt($("#currentPage").html());
				if(currentPage != 1){
					currentPage = 1;
					dataType(currentPage);
				}
			});
			
			//上一页按钮
			$("#upPage").click(function(){
				//获取当前页
				var currentPage = parseInt($("#currentPage").html());
				if(currentPage > 1){
					currentPage--;
					dataType(currentPage);
				}
			});
			
			//下一页按钮
			$("#downPage").click(function(){
				//获取当前页
				var currentPage = parseInt($("#currentPage").html());
				//获取总页
				var totalPage = parseInt($("#totalPage").html());
				if(currentPage < totalPage){
					currentPage++;
					dataType(currentPage);
				}
			});
			
			//末页按钮
			$("#lastPage").click(function(){
				//获取当前页
				var currentPage = parseInt($("#currentPage").html());
				//获取总页
				var totalPage = parseInt($("#totalPage").html());
				if(currentPage != totalPage){
					dataType(totalPage);
				}
			});
			
			//跳转页按钮
			$("#jump").click(function(){
				//获取当前页
				var currentPage = parseInt($("#currentPage").html());
				//获取总页
				var totalPage = parseInt($("#totalPage").html());
				//获取要跳转的页
				var jumpPage = $("#jumpPage").val();
				if(jumpPage != currentPage && jumpPage >= 1 && jumpPage <= totalPage){
					dataType(jumpPage);
				}
			});
			
			//页面订单状态数据显示类型，用于页面按钮操作
			function dataType(page){
				//获取当前订单状态
				var status = $("#status").html();
				switch (status) {
				case "全 部 状 态":
					coachOrder(page,10);
					break;
				case "授 课 中":
					coachOrder(page,0);
					break;
				case "授 课 完 毕":
					coachOrder(page,1);
					break;
				case "交 易 完 成":
					coachOrder(page,2);
					break;
				case "退款申请":
					coachOrder(page,3);
					break;
				case "已 退 款":
					coachOrder(page,4);
					break;
				case "取消申请":
					coachOrder(page,5);
					break;
				case "已 取 消":
					coachOrder(page,6);
					break;
				case "已 拒 绝":
					coachOrder(page,7);
					break;
				}
			}
			/* ------页面按钮控制结束------ */
			
			
			/* ------订单状态选择------ */
			//订单状态选择
			$("#status_10").click(function(){
				coachOrder(1,10);
				$("#status").text("全 部 状 态");
			});
			$("#status_0").click(function(){
				coachOrder(1,0);
				$("#status").text("授 课 中");
			});
			$("#status_1").click(function(){
				coachOrder(1,1);
				$("#status").text("授 课 完 毕");
			});
			$("#status_2").click(function(){
				coachOrder(1,2);
				$("#status").text("交 易 完 成");
			});
			$("#status_3").click(function(){
				coachOrder(1,3);
				$("#status").text("退款申请");
			});
			$("#status_4").click(function(){
				coachOrder(1,4);
				$("#status").text("已 退 款");
			});
			$("#status_5").click(function(){
				coachOrder(1,5);
				$("#status").text("取消申请");
			});
			$("#status_6").click(function(){
				coachOrder(1,6);
				$("#status").text("已 取 消");
			});
			$("#status_7").click(function(){
				coachOrder(1,7);
				$("#status").text("已 拒 绝");
			});
			
			
			/* ------订单页面数据显示开始------ */
			//分页显示教练订单
			function coachOrder(page,status){
				$.ajax({
					url:"order/coachOrder",
					type:"get",
					data:{
						index:(page-1)*10,
						count:10,
						currentPage:page,
						status:status,
						uid:${uid}
					},
					dataType:"json",
					success:function(result){
						//设置数据总条数、当前页和总页数
						setPage(result,page);
						//清空表中数据
						$("#tbody").empty();
						for (var i = 0; i < result.data.length; i++) {
							var order = result.data[i];
							var tr = $("<tr></tr>");
							var createdate = $("<td></td>").text(order.createDate);
							var oid = $("<td></td>").text(order.oid);
							var content = $("<td></td>").text(order.content);
							var sname = $("<td></td>").text(order.sname);
							var price = $("<td></td>").text(order.price);
							/* var status = $("<td></td>").text(order.status); */
							var status = statuss(order);
							var action = actions(order);
							/* var action = $('<td>'
									+'<button type="button" class="btn bg-light">付款</button>'
									+'<button type="button" class="btn bg-light">退款</button>'
									+'<button type="button" class="btn bg-light">删除</button>'
									+'</td>'); */
							tr.append(createdate);
							tr.append(oid);
							tr.append(content);
							tr.append(sname);
							tr.append(price);
							tr.append(status);
							tr.append(action);
							$("#tbody").append(tr);
						}
					}
				});
			}
			
			//设置页面总条数、当前页和总页数
			function setPage(data,page){
				//总条数
				$("#total").html(data.total);
				//当前页
				$("#currentPage").html(page);
				//总页数
				var totalPage = data.total%10==0?data.total/10:(parseInt(data.total/10)+1);
				if(totalPage == 0){
					$("#totalPage").html(1);
				}else{
					$("#totalPage").html(totalPage);
				}
			}
			
			//设置表格中订单显示的状态
			function statuss(order){
				if(order.status == 0){
					var status = $("<td></td>").text("授课中");
				}else if(order.status == 1){
					var status = $("<td></td>").text("授课完毕");
				}else if(order.status == 2){
					var status = $("<td></td>").text("交易完成");
				}else if(order.status == 3){
					var status = $("<td></td>").text("退款申请");
				}else if(order.status == 4){
					var status = $("<td></td>").text("已退款");
				}else if(order.status == 5){
					var status = $("<td></td>").text("取消申请");
				}else if(order.status == 6){
					var status = $("<td></td>").text("已取消");
				}else if(order.status == 7){
					var status = $("<td></td>").text("已拒绝");
				}
				return status;
			}
			
			//设置表格中订单操作类型
			function actions(order){
				if(order.status == 0){
					var action = $('<td>'
							+'<button type="button" class="btn bg-light" onclick="confirmFinishOrder('+order.oid+')">结课</button>'
							+'</td>');
				}else if(order.status == 1){
					var action = $('<td>'
							+'<button type="button" class="btn bg-light" disabled="disabled">授课完毕</button>'
							+'</td>');
				}else if(order.status == 2){
					var action = $('<td>'
							+'<button type="button" class="btn bg-light" disabled="disabled">交易完成</button>'
							+'</td>');
				}else if(order.status == 3){
					var action = $('<td>'
							+'<button type="button" class="btn bg-light" data-toggle="modal" data-target="#myModal" onclick="agreeRefund('+order.oid+','+order.price+',\''+order.sname+'\')">同意</button>'
							+'<button type="button" class="btn bg-light" onclick="refuse('+order.oid+')">拒绝</button>'
							+'</td>');
				}else if(order.status == 4){
					var action = $('<td>'
							+'<button type="button" class="btn bg-light" disabled="disabled">已退款</button>'
							+'</td>');
				}else if(order.status == 5){
					var action = $('<td>'
							+'<button type="button" class="btn bg-light" onclick="agreeCancle('+order.oid+')">同意</button>'
							+'<button type="button" class="btn bg-light" onclick="refuse('+order.oid+')">拒绝</button>'
							+'</td>');
				}else if(order.status == 6){
					var action = $('<td>'
							+'<button type="button" class="btn bg-light" disabled="disabled">已取消</button>'
							+'</td>');
				}else if(order.status == 7){
					var action = $('<td>'
							+'<button type="button" class="btn bg-light" disabled="disabled">已拒绝</button>'
							+'</td>');
				}
				return action;
			}
			/* ------订单页面数据显示结束------ */
			
			
			/* ------订单页面数据操作开始------ */
			//确认完成授课/结课
			function confirmFinishOrder(oid){
				$.ajax({
					url:"order/confirmFinish",
					type:"post",
					data:{
						oid:oid,
						status:1,
						isCoachCheck:1
					},
					success:function(result){
						alert(result);
						if(result.indexOf("成功") != -1){
							//获取当前页
							var currentPage = parseInt($("#currentPage").html());
							dataType(currentPage);
						}
					}
				});
			}			
						
			//同意取消订单
			function agreeCancle(oid){
				$.ajax({
					url:"order/agreeCancle",
					type:"post",
					data:{
						oid:oid,
						status:6
					},
					success:function(result){
						alert(result);
						if(result.indexOf("成功") != -1){
							//获取当前页
							var currentPage = parseInt($("#currentPage").html());
							dataType(currentPage);
						}
					}
				});
			}
			//拒绝
			function refuse(oid){
				$.ajax({
					url:"order/refuse",
					type:"post",
					data:{
						oid:oid,
						status:7
					},
					success:function(result){
						alert(result);
						if(result.indexOf("成功") != -1){
							//获取当前页
							var currentPage = parseInt($("#currentPage").html());
							dataType(currentPage);
						}
					}
				});
			}
			
			//同意退款，跳转密码验证窗口
			function agreeRefund(oid,price,sname){
				$("#cname").text("支付给"+sname);
				$("#price").text(price);
				$("#orderId").val(oid);
				$.ajax({
					url:"order/agreeRefund",
					type:"post",
					data:{
						oid:oid,
						status:4
					},
					dataType:"json",
					success:function(result){
						if(result.indexOf('失败') != -1){
							//获取当前页
							var currentPage = parseInt($("#currentPage").html());
							dataType(currentPage);
						}
					}
				});
			}
			
			//验证密码，进行付款
			$("#confirmPassword").click(function(){
				$.ajax({
					url:"order/payment2",
					type:"get",
					data:{
						oid:$("#orderId").val(),
						password:$("#password").val()
					},
					success:function(result){
						/* alert(result); */
						$("#passwordResult").text(result);
						//清空密码
						$(".password-div ul li").text("");
						if(result.indexOf('成功') != -1){
							location.href = "jsp/studentOrder.jsp";
							//获取当前页
							var currentPage = parseInt($("#currentPage").html());
							dataType(currentPage);
						}
					}
				});
			});
			
			//显示评价信息
			function showComment(oid){
				$.ajax({
					url:"comment/getComment",
					type:"get",
					data:{
						oid:oid
					},
					success:function(result){
						$("#showCommentContent").empty();
						var tr = $('<tr></tr>');
						if(result == null || result == ""){
							tr.append("订单尚未完成，暂无评价！");
							$("#showCommentContent").html(tr);
							return;
						}
						var commentDate = $('<td></td>').text(result.commentDate);
						var commentRank = $('<td></td>').text(result.commentRank+"星");
						var commentContent = $('<td></td>').text(result.commentContent);
						tr.append(commentDate);
						tr.append(commentRank);
						tr.append(commentContent);
						$("#showCommentContent").append(tr);
					}
				});
			}
			
		</script>
</body>
</html>